/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Developer
 */
public class Data {

    public static File f = new File("config.xml");

    public boolean CreateAlbum(String sAlbumName, String sDate, String sPlace, String sAlbumDescription) {
        try {
            Document doc = null;
            Element root = null;
            if (!f.exists()) {
                doc = DocumentBuilderFactory.newInstance().
                        newDocumentBuilder().newDocument();
                root = doc.createElement("Albums");
                doc.appendChild(root);
            } else {
                doc = DocumentBuilderFactory.newInstance().
                        newDocumentBuilder().parse(f);
                root = doc.getDocumentElement();
            }

            Element album = doc.createElement("Album");
            root.appendChild(album);

            // set attribute to staff element
            Attr attr = doc.createAttribute("name");
            attr.setValue(sAlbumName);
            album.setAttributeNode(attr);
            // shorten way
            // staff.setAttribute("id", "1");

            // Name elements
            Element sName = doc.createElement("Name");
            sName.appendChild(doc.createTextNode(sAlbumName));
            album.appendChild(sName);


            // Date elements
            Element Date = doc.createElement("Date");
            Date.appendChild(doc.createTextNode(sDate));
            album.appendChild(Date);

            // PlaceTaken elements
            Element sPlaceTaken = doc.createElement("PlaceTaken");
            sPlaceTaken.appendChild(doc.createTextNode(sPlace));
            album.appendChild(sPlaceTaken);

            // Description elements
            Element sDescription = doc.createElement("Description");
            sDescription.appendChild(doc.createTextNode(sAlbumDescription));
            album.appendChild(sDescription);

            Source source = new DOMSource(doc);
            Result result = new StreamResult(f);
            Transformer trans = TransformerFactory.newInstance().
                    newTransformer();

            trans.transform(source, result);

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }


    }

    public boolean UpdateAlbum(String sOldAlbumName, String sAlbumName) {
       
        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(f);
            doc.getDocumentElement().normalize();

            // Get the staff element by tag name directly
            NodeList nodeLst = doc.getElementsByTagName("Album");

            if (nodeLst != null && nodeLst.getLength() > 0) {
                for (int i = 0; i < nodeLst.getLength(); i++) {
                    Node myNode = nodeLst.item(i);
                    if (myNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element albumElement = (Element) myNode;


                        if (albumElement.getAttribute("name").equals(sOldAlbumName)) {
                            NamedNodeMap attr = myNode.getAttributes();
                            Node nodeAttr = attr.getNamedItem("name");
                            nodeAttr.setTextContent(sAlbumName);
                            albumElement.getElementsByTagName("Name").item(0).setTextContent(sAlbumName);
                            i = nodeLst.getLength();
                        }


                    }

                }
            }
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(f);
            transformer.transform(source, result);

            System.out.println("Done");
            return true;

        } catch (Exception pce) {

            pce.printStackTrace();
            return false;
        }

    }

    public String[] GetAllAlbumName() {
        String[] arrName=null;
        if (!f.exists()){
        return arrName;
        }
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(f);

            doc.getDocumentElement().normalize();

            //      System.out.println("Root element : "+ doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("Album");

            if (nList != null && nList.getLength() > 0) {
                arrName = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    // System.out.println("\nCurrent Element : " + nNode.getNodeName());
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;

                        System.out.println("Ten Album : " + eElement.getAttribute("name"));
                        arrName[temp] = eElement.getAttribute("name");
                        /*    System.out.println("Author : "
                         + eElement.getElementsByTagName("author").item(0)
                         .getTextContent());
                         System.out.println("Title : "
                         + eElement.getElementsByTagName("title").item(0)
                         .getTextContent());
                         System.out.println("Genre : "
                         + eElement.getElementsByTagName("genre").item(0)
                         .getTextContent());
                         System.out.println("Price : "
                         + eElement.getElementsByTagName("price").item(0)
                         .getTextContent());*/

                    }
                }

            }
            return arrName;
        } catch (Exception e) {
            e.printStackTrace();
            return arrName;
        }

    }

    public List<String> LoadImagesbyAlbumName(String sAlbumName) {
        
        List<String> lstImage = new ArrayList<String>();
        try {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(f);
            doc.getDocumentElement().normalize();
            //  System.out.println("Root: "+ doc.getDocumentElement().getNodeName() + "\n");
            NodeList nodeLst = doc.getElementsByTagName("Album");
            if (nodeLst != null && nodeLst.getLength() > 0) {
                for (int i = 0; i < nodeLst.getLength(); i++) {
                    Node myNode = nodeLst.item(i);
                    if (myNode.getNodeType() == Node.ELEMENT_NODE) {
                        /**
                         * Product ID*
                         */
                        Element albumElement = (Element) myNode;
                        if (albumElement.getAttribute("name").equals(sAlbumName)) {
                            NodeList imageLst = albumElement.getElementsByTagName("image");
                            if (imageLst != null && imageLst.getLength() > 0) {
                                for (int j = 0; j < imageLst.getLength(); j++) {
                                    Node imageNode = imageLst.item(j);
                                    //System.out.println("photo path : " + imageNode.getTextContent());
                                    lstImage.add(imageNode.getTextContent());
                                }
                            }
                            i = nodeLst.getLength();
                        }






                    }

                }
            }

        } catch (Exception e) {
            e.printStackTrace();


        }
        return lstImage;
    }

    public String[] GetAlbumInfobyAlbumName(String sAlbumName) {

        String[] arrAlbumInfo = new String[4];
        try {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(f);
            doc.getDocumentElement().normalize();
            //  System.out.println("Root: "+ doc.getDocumentElement().getNodeName() + "\n");
            NodeList nodeLst = doc.getElementsByTagName("Album");
            if (nodeLst != null && nodeLst.getLength() > 0) {
                for (int i = 0; i < nodeLst.getLength(); i++) {
                    Node myNode = nodeLst.item(i);
                    if (myNode.getNodeType() == Node.ELEMENT_NODE) {
                        /**
                         * Product ID*
                         */
                        Element albumElement = (Element) myNode;



                        if (albumElement.getAttribute("name").equals(sAlbumName)) {

                            arrAlbumInfo[0] = (albumElement.getElementsByTagName("Name").item(0).getTextContent());
                            arrAlbumInfo[1] = (albumElement.getElementsByTagName("Date").item(0).getTextContent());
                            arrAlbumInfo[2] = (albumElement.getElementsByTagName("PlaceTaken").item(0).getTextContent());
                            arrAlbumInfo[3] = (albumElement.getElementsByTagName("Description").item(0).getTextContent());
                            i = nodeLst.getLength();
                        }

                    }



                }
            }
        } catch (Exception e) {
            e.printStackTrace();


        }
        return arrAlbumInfo;
    }

    public boolean DeleteAlbum(String sAlbumName) {
      
        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(f);
            doc.getDocumentElement().normalize();

            // Get the staff element by tag name directly
            NodeList nodeLst = doc.getElementsByTagName("Album");

            if (nodeLst != null && nodeLst.getLength() > 0) {
                for (int i = 0; i < nodeLst.getLength(); i++) {
                    Node myNode = nodeLst.item(i);
                    if (myNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element albumElement = (Element) myNode;


                        if (albumElement.getAttribute("name").equals(sAlbumName)) {
                            myNode.getParentNode().removeChild(myNode);
                            i = nodeLst.getLength();
                        }


                    }

                }
            }
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(f);
            transformer.transform(source, result);

            System.out.println("Done");
            return true;

        } catch (Exception pce) {

            pce.printStackTrace();
            return false;
        }

    }

    public boolean AddPhototoAlbum(String sAlbumName, String[] arrURL) {
     
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(f);
            doc.getDocumentElement().normalize();

            // Get the staff element by tag name directly
            NodeList nodeLst = doc.getElementsByTagName("Album");

            if (nodeLst != null && nodeLst.getLength() > 0) {
                for (int i = 0; i < nodeLst.getLength(); i++) {
                    Node myNode = nodeLst.item(i);
                    if (myNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element albumElement = (Element) myNode;


                        if (albumElement.getAttribute("name").equals(sAlbumName)) {
                            for (int k = 0; k < arrURL.length; k++) {
                                Element elementImage = doc.createElement("image");
                                elementImage.appendChild(doc.createTextNode(arrURL[k]));
                                myNode.appendChild(elementImage);
                            }
                            i = nodeLst.getLength();
                        }
                    }
                }
            }
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(f);
            transformer.transform(source, result);

            System.out.println("Add photo done");
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean DeletePhoto(String sAlbumName, String sPhotoPath) {
        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(f);
            doc.getDocumentElement().normalize();

            // Get the staff element by tag name directly
            NodeList nodeLst = doc.getElementsByTagName("Album");

            if (nodeLst != null && nodeLst.getLength() > 0) {
                for (int i = 0; i < nodeLst.getLength(); i++) {
                    Node myNode = nodeLst.item(i);
                    if (myNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element albumElement = (Element) myNode;


                        if (albumElement.getAttribute("name").equals(sAlbumName)) {
                            //  myNode.getParentNode().removeChild(myNode);
                            NodeList imageLst = albumElement.getElementsByTagName("image");
                            if (imageLst != null && imageLst.getLength() > 0) {
                                for (int j = 0; j < imageLst.getLength(); j++) {
                                    Node imageNode = imageLst.item(j);
                                    //System.out.println("photo path : " + imageNode.getTextContent());
                                    if (imageNode.getTextContent().equals(sPhotoPath)) {
                                        imageNode.getParentNode().removeChild(imageNode);
                                        System.out.println("Delete Photo Done");
                                        j = imageLst.getLength();
                                        i = nodeLst.getLength();
                                    }
                                }
                            }

                        }


                    }

                }
            }
            // write the content into xml file
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(f);
            transformer.transform(source, result);


            return true;

        } catch (Exception pce) {

            pce.printStackTrace();
            return false;
        }

    }
     public String GetFolderPath(String sFolderName) {
 if (!f.exists()){
        return null;
        }
       String sPath=null;
        try {
            
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(f);
            doc.getDocumentElement().normalize();
            //  System.out.println("Root: "+ doc.getDocumentElement().getNodeName() + "\n");
            NodeList nodeLst = doc.getElementsByTagName("Folder");
            if (nodeLst != null && nodeLst.getLength() > 0) {
                for (int i = 0; i < nodeLst.getLength(); i++) {
                    Node myNode = nodeLst.item(i);
                    if (myNode.getNodeType() == Node.ELEMENT_NODE) {
                        /**
                         * Product ID*
                         */
                        Element albumElement = (Element) myNode;



                        if (albumElement.getAttribute("name").equals(sFolderName)) {

                            sPath= (albumElement.getElementsByTagName("Path").item(0).getTextContent());
                            
                        }

                    }



                }
            }
        } catch (Exception e) {
            e.printStackTrace();

        
        }
        return  sPath;
        
    }
    public List<String> LoadImagesinFolder(String sFolder) {
        String path = GetFolderPath(sFolder);
        if(path==null)
        {
        return null;
        }
        List<String> lstFile = new ArrayList<String>();
        String files;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        if(listOfFiles!=null &&listOfFiles.length>0){
        for (int i = 0; i < listOfFiles.length; i++) {

            if (listOfFiles[i].isFile()) {
                files = listOfFiles[i].getName();
                if (files.endsWith(".jpg") || files.endsWith(".JPG")) {
                    lstFile.add(listOfFiles[i].getPath());
                }
            }
        }
        }
        return lstFile;
    }
      public boolean CreateFolder(String sFolderPath,String sFolderName ) {
        try {
            Document doc = null;
            Element root = null;
            if (!f.exists()) {
                doc = DocumentBuilderFactory.newInstance().
                        newDocumentBuilder().newDocument();
                root = doc.createElement("Albums");
                doc.appendChild(root);
            } else {
                doc = DocumentBuilderFactory.newInstance().
                        newDocumentBuilder().parse(f);
                root = doc.getDocumentElement();
            }

            Element album = doc.createElement("Folder");
            root.appendChild(album);

            // set attribute to staff element
            Attr attr = doc.createAttribute("name");
            attr.setValue(sFolderName);
            album.setAttributeNode(attr);
            // shorten way
            // staff.setAttribute("id", "1");

            // Name elements
            Element sName = doc.createElement("Path");
            sName.appendChild(doc.createTextNode(sFolderPath));
            album.appendChild(sName);


            Source source = new DOMSource(doc);
            Result result = new StreamResult(f);
            Transformer trans = TransformerFactory.newInstance().
                    newTransformer();

            trans.transform(source, result);

            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }


    }
       public String[] GetAllFolder() {
           if (!f.exists()){
        return null;
        }
        String[] arrName=null;
        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(f);

            doc.getDocumentElement().normalize();

                  System.out.println("Root element : "+ doc.getDocumentElement().getNodeName());

            NodeList nList = doc.getElementsByTagName("Folder");

            if (nList != null && nList.getLength() > 0) {
                arrName = new String[nList.getLength()];
                for (int temp = 0; temp < nList.getLength(); temp++) {
                    Node nNode = nList.item(temp);
                    // System.out.println("\nCurrent Element : " + nNode.getNodeName());
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                        Element eElement = (Element) nNode;

                      //  System.out.println("Ten Album : " + eElement.getAttribute("name"));
                        arrName[temp] = eElement.getAttribute("name");
                       
                    }
                }

            }
            return arrName;
        } catch (Exception e) {
            e.printStackTrace();
            return arrName;
        }

    }

}
